﻿using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using StartsEAS_API.BLL.Interfaces.IUserB;
using StartsEAS_API.DAL.UserD;
using StartsEAS_API.Model;
using StartsEAS_API.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StartsEAS_API.BLL.UserB
{
    public class UserBLL : IUserBLL
    {
        private readonly StarsEASContext _context;
        public UserDAL _userDal;
        public UserBLL(IServiceScopeFactory context)
        {
            _context = context.CreateScope().ServiceProvider.GetRequiredService<StarsEASContext>();
            _userDal = new(_context);
        }

        public ResultUtils ForgotPassword(string idcard, string newPwd, string userName)
        {
            return _userDal.ForgotPassword(idcard, newPwd, userName);
        }

        public ResultUtils GetAllUser()
        {
            return _userDal.GetAllUser();
        }

        public ResultUtils InsertUser(JObject data)
        {
            string jsonStr = JsonConvert.SerializeObject(data);
            User user = JsonConvert.DeserializeObject<User>(jsonStr);
            return _userDal.InsertUser(user);
        }

        public ResultUtils UpdatePassword(string oldPwd, string newPwd, string userName)
        {
            return _userDal.UpdatePassword(oldPwd, newPwd, userName);
        }
    }
}
